package chat.server

import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter
import java.net.ServerSocket
import java.net.Socket
import java.util.concurrent.CopyOnWriteArrayList

var socketList= ArrayList<Socket>()

fun main(args: Array<String>) {
    val serverSocket = ServerSocket(12345)
    while (true) {
        val socket: Socket = serverSocket.accept()
        socketList.add(socket)
        Thread(object : Runnable {
            override fun run() {
                val br = BufferedReader(InputStreamReader(socket.getInputStream()))
                while (true) {
                    val str = br.readLine()
                    printFunc(str)
                }
            }
        }).start()
    }
}

fun printFunc(str:String){
    for (socket in socketList) {
        val pw = PrintWriter(socket.getOutputStream())
        pw.println("" + socket.inetAddress + "-" + socket.port + "：" + str)
        pw.flush()
    }
}
